import React, {useState} from 'react';
import {Radio, Input, Button, message, Divider} from 'antd';
import CryptoJS from 'crypto-js';

const {TextArea} = Input;

const CryptoTool = () => {
    //加密解密和hash
    const [selectedTab, setSelectedTab] = useState('hash');
    const [selectedAlgorithm, setSelectedAlgorithm] = useState('SHA1');
    const [inputText, setInputText] = useState('');
    const [outputText, setOutputText] = useState('');

    const handleTabChange = e => {
        setSelectedTab(e.target.value);
    };

    const handleAlgorithmChange = e => {
        setSelectedAlgorithm(e.target.value);
    };

    const handleInputChange = e => {
        setInputText(e.target.value);
    };

    const handleConvertClick = () => {
        if (selectedTab === 'hash') {
            let hash;
            if (selectedAlgorithm === 'SHA1') {
                hash = CryptoJS.SHA1(inputText).toString();
            } else if (selectedAlgorithm === 'SHA256') {
                hash = CryptoJS.SHA256(inputText).toString();
            } else if (selectedAlgorithm === 'SHA512') {
                hash = CryptoJS.SHA512(inputText).toString();
            }
            setOutputText(hash);
        } else if (selectedTab === 'encrypt') {
            let ciphertext;
            if (selectedAlgorithm === 'AES') {
                ciphertext = CryptoJS.AES.encrypt(
                    inputText,
                    'secret key 123'
                ).toString();
            } else if (selectedAlgorithm === 'DES') {
                ciphertext = CryptoJS.DES.encrypt(
                    inputText,
                    'secret key 123'
                ).toString();
            } else if (selectedAlgorithm === 'Base64') {
                ciphertext = window.btoa(inputText);
            }
            setOutputText(ciphertext);
        } else if (selectedTab === 'decrypt') {
            let plaintext;
            if (selectedAlgorithm === 'AES') {
                const bytes = CryptoJS.AES.decrypt(inputText, 'secret key 123');
                plaintext = bytes.toString(CryptoJS.enc.Utf8);
            } else if (selectedAlgorithm === 'DES') {
                const bytes = CryptoJS.DES.decrypt(inputText, 'secret key 123');
                plaintext = bytes.toString(CryptoJS.enc.Utf8);
            } else if (selectedAlgorithm === 'Base64') {
                plaintext = window.atob(inputText);
            }
            setOutputText(plaintext);
        }
    };
    //乘法逆元计算
    const [a, setA] = useState('');
    const [m, setM] = useState('');
    const [result, setResult] = useState('');

    const handleCalculation = () => {
        const numA = parseInt(a);
        const numM = parseInt(m);

        if (isNaN(numA) || isNaN(numM) || numM <= 0) {
            setResult('请输入正确的数字！');
            return;
        }

        let [gcd, x, y] = extendedEuclideanAlgorithm(numA, numM);

        if (gcd !== 1) {
            setResult(`a关于模数m不存在乘法逆元`);
        } else {
            setResult(`a关于模数m的乘法逆元为: ${mod(x, numM)}`);
        }
    };

    const extendedEuclideanAlgorithm = (a, b) => {
        if (b === 0) {
            return [a, 1, 0];
        }

        let [gcd, x1, y1] = extendedEuclideanAlgorithm(b, a % b);
        let x = y1;
        let y = x1 - Math.floor(a / b) * y1;

        return [gcd, x, y];
    };

    const mod = (a, b) => {
        const r = a % b;
        return r < 0 ? r + b : r;
    };

    return (
        <div>
            <Divider>加密解密</Divider>
            <Radio.Group
                value={selectedTab}
                onChange={handleTabChange}>
                <Radio.Button value='hash'>Hash编码</Radio.Button>
                <Radio.Button value='encrypt'>加密</Radio.Button>
                <Radio.Button value='decrypt'>解密</Radio.Button>
            </Radio.Group>
            <br/>
            <br/>
            {selectedTab === 'hash' && (
                <Radio.Group
                    value={selectedAlgorithm}
                    onChange={handleAlgorithmChange}>
                    <Radio.Button value='SHA1'>SHA1</Radio.Button>
                    <Radio.Button value='SHA256'>SHA256</Radio.Button>
                    <Radio.Button value='SHA512'>SHA256</Radio.Button>
                </Radio.Group>
            )}
            {selectedTab !== 'hash' && (
                <Radio.Group
                    value={selectedAlgorithm}
                    onChange={handleAlgorithmChange}>
                    <Radio.Button value='AES'>AES</Radio.Button>
                    <Radio.Button value='DES'>DES</Radio.Button>
                    <Radio.Button value='Base64'>Base64</Radio.Button>
                </Radio.Group>
            )}
            <TextArea
                placeholder='输入要处理的文本'
                rows={4}
                onChange={handleInputChange}
            />
            <Button
                type='primary'
                onClick={handleConvertClick}>
                转换
            </Button>
            <br/>
            <br/>
            <TextArea
                placeholder='输出结果'
                rows={4}
                value={outputText}
                readOnly
            />
            <Divider>计算乘法逆元</Divider>
            <div>
                <Input
                    placeholder='a'
                    value={a}
                    onChange={e => setA(e.target.value)}
                    style={{width: 100, marginRight: 10}}
                />
                <span>mod</span>
                <Input
                    placeholder='m'
                    value={m}
                    onChange={e => setM(e.target.value)}
                    style={{width: 100, marginLeft: 10, marginRight: 10}}
                />
                <Button
                    type='primary'
                    onClick={handleCalculation}>
                    计算
                </Button>
                <div style={{marginTop: 10}}>{result}</div>
            </div>
        </div>
    );
};

export default CryptoTool;

// import React, { useState } from 'react';
// import { Radio, Input, Button } from 'antd';
// import CryptoJS from 'crypto-js';

// const { TextArea } = Input;

// const CryptoTool = () => {
//   const [selectedTab, setSelectedTab] = useState('hash');
//   const [selectedAlgorithm, setSelectedAlgorithm] = useState('SHA1');
//   const [inputText, setInputText] = useState('');
//   const [outputText, setOutputText] = useState('');

//   const handleTabChange = e => {
//     setSelectedTab(e.target.value);
//   };

//   const handleAlgorithmChange = e => {
//     setSelectedAlgorithm(e.target.value);
//   };

//   const handleInputChange = e => {
//     setInputText(e.target.value);
//   };

//   const handleConvertClick = () => {
//     if (selectedTab === 'hash') {
//       let hash;
//       if (selectedAlgorithm === 'SHA1') {
//         hash = CryptoJS.SHA1(inputText).toString();
//       } else if (selectedAlgorithm === 'SHA256') {
//         hash = CryptoJS.SHA256(inputText).toString();
//       } else if (selectedAlgorithm === 'SHA512') {
//         hash = CryptoJS.SHA512(inputText).toString();
//       }
//       setOutputText(hash);
//     } else if (selectedTab === 'encrypt') {
//       let ciphertext;
//       if (selectedAlgorithm === 'AES') {
//         ciphertext = CryptoJS.AES.encrypt(
//           inputText,
//           'secret key 123'
//         ).toString();
//       } else if (selectedAlgorithm === 'DES') {
//         ciphertext = CryptoJS.DES.encrypt(
//           inputText,
//           'secret key 123'
//         ).toString();
//       } else if (selectedAlgorithm === 'Vigenere') {
//         ciphertext = CryptoJS.Vigenere.encrypt(
//           inputText,
//           'secret key 123'
//         ).toString();
//       }
//       setOutputText(ciphertext);
//     } else if (selectedTab === 'decrypt') {
//       let bytes;
//       if (selectedAlgorithm === 'AES') {
//         bytes = CryptoJS.AES.decrypt(inputText, 'secret key 123');
//       } else if (selectedAlgorithm === 'DES') {
//         bytes = CryptoJS.DES.decrypt(inputText, 'secret key 123');
//       } else if (selectedAlgorithm === 'Base64') {
//         plaintext = window.atob(inputText);
//       }
//       const plaintext = bytes.toString(CryptoJS.enc.Utf8);
//       setOutputText(plaintext);
//     }
//   };

//   return (
//     <div>
//       <Radio.Group
//         value={selectedTab}
//         onChange={handleTabChange}>
//         <Radio.Button value='hash'>Hash编码</Radio.Button>
//         <Radio.Button value='encrypt'>加密</Radio.Button>
//         <Radio.Button value='decrypt'>解密</Radio.Button>
//       </Radio.Group>
//       <br />
//       <br />
//       {selectedTab !== 'hash' && (
//         <Radio.Group
//           value={selectedAlgorithm}
//           onChange={handleAlgorithmChange}>
//           <Radio.Button value='AES'>AES</Radio.Button>
//           <Radio.Button value='DES'>DES</Radio.Button>
//           <Radio.Button value='Base64'>Base64</Radio.Button>
//         </Radio.Group>
//       )}
//       <TextArea
//         placeholder='输入要处理的文本'
//         rows={4}
//         onChange={handleInputChange}
//       />
//       <Button
//         type='primary'
//         onClick={handleConvertClick}>
//         转换
//       </Button>
//       <br />
//       <br />
//       <TextArea
//         value={outputText}
//         rows={4}
//         readOnly
//       />
//     </div>
//   );
// };

// export default CryptoTool;
